var gulp = require('gulp'),
    concat = require('gulp-concat'),
    rename = require('gulp-rename'),

    del = require('del'),
    cache = require('gulp-cache'),

    htmlmin = require('gulp-htmlmin'),

    uglify = require('gulp-uglify'),
    jshint = require('gulp-jshint'),
    gulp = require('gulp'),
    babel = require('gulp-babel'),
    order = require('gulp-order'),

    minifycss = require('gulp-minify-css'),
    autoprefixer = require('gulp-autoprefixer'),
    less = require('gulp-less'),
    notify = require('gulp-notify'),
    plumber = require('gulp-plumber'),
    sourcemaps = require('gulp-sourcemaps'),

    imagemin = require('gulp-imagemin'),
    pngquant = require('imagemin-pngquant')

gulp.task('build', ['clear', 'jshint'], function () {
    gulp.start('html', 'css', 'js', 'images','font')
});

gulp.task('jshint', function () {
    return gulp.src('js/**/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'))
})

gulp.task('watch', function () {
    gulp.watch('./src/**/*', ['build'])
    gulp.watch('./index.html', ['build'])
})

gulp.task('html', function () {
    return gulp.src('./src/index.html')
        .pipe(htmlmin({
            removeComments: true,//清除HTML注释
            collapseWhitespace: true,//压缩HTML
            removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
            removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
            removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
            minifyJS: true,//压缩页面JS
            minifyCSS: true//压缩页面CSS
        }))
        .pipe(gulp.dest('./dist'))
})

gulp.task('css', function () {
    return gulp.src(['./src/css/**/*.less','./src/css/**/*.css'])
        .pipe(order([
            "jquery.fullpage.min.css",
            "font-awesome.min.css",
            "main.less"
        ]))
        .pipe(concat('app.css'))
        .pipe(sourcemaps.init())
        .pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
        .pipe(less())
        .pipe(sourcemaps.write())
        .pipe(autoprefixer({
            browsers: ['last 2 versions', 'Android >= 4.0'],
            cascade: true, //是否美化属性值
            remove: true //是否去掉不必要的前缀 默认：true
        }))
        .pipe(minifycss())
        .pipe(rename('app.min.css'))
        .pipe(gulp.dest('./dist/css'))
})

gulp.task('js', function () {
    return gulp.src('./src/js/**/*.js')
        .pipe(order([
            "jquery.min.js",
            "jquery.easings.min.js",
            "scrolloverflow.min.js",
            "jquery.fullpage.min.js",
            "main.js"
        ]))
        .pipe(concat('app.js'))
        .pipe(sourcemaps.init())
        .pipe(babel({
            presets: ['env','es2015']
        }))
        .pipe(uglify({
            mangle: true,//类型：Boolean 默认：true 是否修改变量名
            compress: true,//类型：Boolean 默认：true 是否完全压缩
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(rename('app.min.js'))
        .pipe(gulp.dest('./dist/js'))
})

gulp.task('images', function () {
    return gulp.src('./src/images/*.{png,jpg,gif,ico}')
        .pipe(cache(imagemin({
            optimizationLevel: 5, //类型：Number  默认：3  取值范围：0-7（优化等级）
            progressive: true, //类型：Boolean 默认：false 无损压缩jpg图片
            interlaced: true, //类型：Boolean 默认：false 隔行扫描gif进行渲染
            multipass: true, //类型：Boolean 默认：false 多次优化svg直到完全优化
            svgoPlugins: [{removeViewBox: false}],//不要移除svg的viewbox属性
            use: [pngquant()] //使用pngquant深度压缩png图片的imagemin插件
        })))
        .pipe(gulp.dest('./dist/images'))
})

gulp.task('font',function () {
    return gulp.src('./src/fonts/*')
        .pipe(gulp.dest('./dist/fonts'))
})

gulp.task('clear', function () {
    return del('./dist/**/*');
})
